package com.edu.neusoft.users.controller;

import com.edu.neusoft.users.domain.Comment;
import com.edu.neusoft.users.service.ICommentService;
import com.edu.neusoft.users.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/comment")
@Api(value = "CommentController" , tags = {"评论管理接口"})
public class CommentController {
    @Autowired
    private ICommentService commentService;

    @GetMapping("/list")
    @ApiOperation(value = "获取所有评论" , notes = "返回所有评论的列表信息")
    public AjaxResult list(){
        List<Comment> commentList = commentService.selectCommentList();
        return AjaxResult.success(commentList);

    }

    // 根据ID获取评论
    @GetMapping("/{UserID}")
    @ApiOperation(value = "根据ID获取评论" , notes = "返回所获取的评论信息")
    public AjaxResult getCommentByID(@PathVariable("UserID") Long UserID) {
        Comment comment = commentService.getCommentByID(UserID);
        return comment !=null ? AjaxResult.success(comment) : AjaxResult.error("评论不存在");
    }

    // 发布新的评论
    @PostMapping("/createComment")
    @ApiOperation(value = "创建评论" , notes = "根据提供评论的信息创建评论")
    public AjaxResult createComment(@RequestBody Comment comment) {
        int result = commentService.insertComment(comment);

        if (result>0)
        {
            return AjaxResult.success("发布信息成功");
        }
        else {
            return AjaxResult.error("发布信息失败");
        }
    }

    // 更新评论
    @PutMapping("/updateComment")
    @ApiOperation(value = "更新评论信息", notes = "根据提供的信息更新评论")
    public AjaxResult updateComment( @RequestBody Comment comment) {
        int result = commentService.updateComment(comment);

        if (result>0)
        {
            return AjaxResult.success("更新评论成功");
        }
        else {
            return AjaxResult.error("更新评论失败");
        }
    }

    // 删除用户发布的评论
    @DeleteMapping("/{UserID}")
    @ApiOperation(value = "删除评论", notes = "根据ID删除评论")
    public AjaxResult deleteComment(@PathVariable Long UserID) {
        Long result = commentService.deleteComment(UserID);
        return result !=null ? AjaxResult.success(result) : AjaxResult.error("评论不存在");
    }
}